﻿using System;

namespace YatFram
{
    public class YatFram<TContext> : IDisposable where TContext : IContext
    {
        protected YatFram(TContext context)
        {
            Context = context;
        }

        /// <summary>
        /// Given will reset the context. Do not use more than once during a test
        /// </summary>
        public YatFram<TContext> Given { get { Context.Reset(); return this; } }
        public YatFram<TContext> When { get { return this; } }
        public YatFram<TContext> Then { get { return this; } }
        public YatFram<TContext> I { get { return this; } }
        public YatFram<TContext> Am { get { return this; } }
        public YatFram<TContext> Should { get { return this; } }
        public YatFram<TContext> My { get { return this; } }
        public YatFram<TContext> And { get { return this; } }

        public TContext Context { get; set; }

        public YatFram<TContext> Setup(Action<TContext> arrangment)
        {
            return DoItNow(arrangment);
        }

        public YatFram<TContext> Do(Action<TContext> action)
        {
            return DoItNow(action);
        }

        public YatFram<TContext> Check(Action<TContext> assertion)
        {
            return DoItNow(assertion);
        }

        private YatFram<TContext> DoItNow(Action<TContext> action)
        {
            try
            {
                action(Context);
            }
            catch (Exception)
            {
                Context.Dump();
                throw;
            }
            return this;
        }

        public void Dispose()
        {
            Context.Dispose();
        }
       
    }
}
